package com.shuai.utils.tree;

import com.shuai.model.po.SysPermission;
import com.shuai.model.vo.SysRouterVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Tree工具类
 *
 * @author shuai
 * @since 2022
 */
public class TreeUtils {

    public static List<SysRouterVO> makeRouter(List<SysPermission> menuList, String pid){
        List<SysRouterVO> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getPid().equals(pid))
                .forEach(item -> {
                    SysRouterVO router = new SysRouterVO();
                    router.setName(item.getName());
                    router.setPath(item.getPath());
                    if("#".equals(item.getPid())){
                        router.setComponent("Layout");
                        router.setAlwaysShow(true);
                    }else{
                        router.setComponent(item.getUrl());
                        router.setAlwaysShow(false);
                    }
                    //设置meta
                    router.setMeta(router.new Meta(
                            item.getLabel(),
                            item.getIcon(),
                            item.getCode().split(",")
                    ));
                    //设置children,每一项的下级
                    List<SysRouterVO> children = makeRouter(menuList, String.valueOf(item.getId()));
                    router.setChildren(children);
                    list.add(router);
                });
        return list;
    }
}
